// pages/invoiceHis/invoiceHis.js
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    type:0,
    price:100
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    //获取手机高度
    that.setData({
      navH: app.globalData.navHeight,
      token: wx.getStorageSync("token")
    });
    wx.request({
      url: app.globalData.url + 'getRechargeListByUserId',
      method: "post",
      data: {
      },
      header: {
        "content-type": "application/x-www-form-urlencoded"
      },
      success: function (res) {
        if (res.data.result == 1) {
          that.setData({
            recharge: res.data.data,
            rechargemoney: res.data.data[0].rechargeMoney,
          });
        }
      }
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  choose:function(e){ 
      this.setData({
        type: e.currentTarget.dataset.type,
        price: e.currentTarget.dataset.price,
        rechargemoney: e.currentTarget.dataset.rechargemoney,
      })
  },
  navBack: function () {
    wx.navigateBack({

    })
  },
  /**充值 */
  one: function (e) {
    var that = this;
    var userid = wx.getStorageSync("userId");
    var price = that.data.price;
    wx.request({
      url: app.globalData.url + 'getWeixinPrepayId',
      data: {
        userId: userid,
        ruleId: that.data.recharge[that.data.type].ruleId
      },
      method: "POST",
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success: function (res) {
        var resultData = res.data;
        console.info(resultData);
        if (res.data.result != 1 && res.data.result != 9) {
          wx.showToast({
            title: "数据异常，请稍后重试",
            icon: 'none'
          })
          return;
        }
        if (res.data.result == 1) {
          if (res.data.token != "" && res.data.token != undefined) {
            wx.setStorageSync("token", res.data.token)
          }
          that.setData({
            hisId:res.data.hisId
          })
        }

        if (res.data.result == 9) {
          wx.reLaunch({
            url: '/pages/zcuslist/index',
          })
          return false;
        }
        wx.requestPayment({
          'timeStamp': resultData.timeStamp,
          'nonceStr': resultData.nonceString,
          'package': resultData.packageStr,
          'signType': resultData.signType,
          'paySign': resultData.sign,
          //支付成功
          success: function (res) {
            console.info(res);
            if ("requestPayment:ok" === res.errMsg) {

              wx.showToast({
                title: "支付成功",
                icon: 'none'
              })

            }
          },
          //支付或者取消失败
          //支付或者取消失败
          fail: function (res) {
            if ("requestPayment:fail cancel" === res.errMsg) {
              wx.showToast({
                title: '取消支付',
                icon: 'none',
                duration: 2000,
                success: function () {
                  if (that.data.type == 2) {
                    wx.navigateBack({
                    });
                    wx.request({
                      url: app.globalData.url + 'cancelThePayment',
                      method: 'GET',
                      data: {
                        hisId: that.data.hisId
                      },
                      success: function (res) {
                        wx.navigateBack({ // 返回首页
                          delta: 1,
                        })
                      }
                    });
                  } else {
                    wx.showToast({
                      title: "取消支付",
                      icon: 'none'
                    })
                    wx.navigateBack({
                      delta: 1,
                    });
                    // setTimeout(function () {
                    //   wx.navigateBack({
                    //     delta: 1,
                    //   });
                    // }, 2000) //延迟时间
                  }

                }
              })
            } else {

              wx.showToast({
                title: "支付失败",
                icon: 'none'
              })
            }
          },
          'complete': function (res) {
            wx.navigateBack({ // 返回首页
              delta: 2,
            })
          }
        })
      }
    })
  },
})
